//
//  WDLoancdetailCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/1/23.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDLoandetailCell.h"
#import "WDMaterialDetail.h"
#import "WDData.h"
#import "WDDetailImageCell.h"

#define HEADER_HEIGHT 100
#define cellItemW ([UIScreen mainScreen].bounds.size.width - 80) / 2
#define cellItemH 102

@interface WDLoandetailCell () <UICollectionViewDelegate, UICollectionViewDataSource>
/** 左边圆点 */
@property (nonatomic, strong) UIImageView *pointView;
/** 左边线 */
@property (nonatomic, strong) UIImageView *lineView;
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 缩略图片 */
@property (nonatomic, strong) UIImageView *photoView;
/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic,assign)CGFloat hightED;


@end

static NSString *cellID = @"detailImageCell";

@implementation WDLoandetailCell

//懒加载collectionView
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //滚动方向（竖直方向）
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 10;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _collectionView.scrollEnabled = NO;
        
        _collectionView.delegate = self;
        
        _collectionView.dataSource = self;
        
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([WDDetailImageCell class]) bundle:nil];
        
        [_collectionView registerNib:nib forCellWithReuseIdentifier:cellID];
        
        _collectionView.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview:_collectionView];
        
    }
    
    return _collectionView;
}

+ (instancetype)loanDetailCellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"loanDetailCell";
    
    WDLoandetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        
        cell = [[WDLoandetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //拍摄资料列表
        UIImageView *pointView = [[UIImageView alloc] init];
        
        [pointView sizeToFit];
        
        [self.contentView addSubview:pointView];
        self.pointView = pointView;
        if (self.isFirstCell) {
            [pointView makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(10);
                make.top.equalTo(23);
                
            }];
        } else {
            [pointView makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(10);
                make.top.equalTo(0);
                
            }];
        }
        
        [pointView layoutIfNeeded];
        
        
        UIImageView *lineView = [[UIImageView alloc] init];
        lineView.image = [UIImage imageNamed:@"single_line"];
        [self.contentView addSubview:lineView];
        self.lineView = lineView;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = ColorForRGB(0x6b6b6b);
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        [titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(pointView.right).offset(0);
            make.centerY.equalTo(pointView.centerY);
        }];
        
    }
    return self;
}

- (void)setMaterialDetail:(WDMaterialDetail *)materialDetail
{
    _materialDetail = materialDetail;
    self.titleLabel.text = materialDetail.name;
    
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(10);
        make.top.equalTo(self.pointView.bottom);
        make.bottom.equalTo(self.contentView.bottom);
    }];
}

- (void)setImagesData:(NSArray *)imagesData
{
    _imagesData = imagesData;
    
    self.collectionView.frame = CGRectMake(35, 30, WDScreenW - 70, self.materialDetail.DetailCellH + 20);
    
    //刷新collectionView
    [self.collectionView reloadData];
}

- (void)setFirstCell:(BOOL)firstCell
{
    _firstCell = firstCell;
    if (firstCell && self.lastCell) {
        self.pointView.image = [UIImage imageNamed:@"redPoint"];
        self.lineView.hidden = YES;
    } else if (firstCell) {
        self.pointView.image = [UIImage imageNamed:@"first_red"];
        self.lineView.hidden = NO;
    } else if (self.isLastCell){
        self.pointView.image = [UIImage imageNamed:@"last_red"];
        self.lineView.hidden = YES;
    } else {
        self.pointView.image = [UIImage imageNamed:@"middle_red"];
        self.lineView.hidden = NO;
    }
    
}

- (void)setLastCell:(BOOL)lastCell
{
    _lastCell = lastCell;
    if (self.firstCell && lastCell) {
        self.pointView.image = [UIImage imageNamed:@"redPoint"];
        self.lineView.hidden = YES;
    } else if (lastCell) {
        self.pointView.image = [UIImage imageNamed:@"last_red"];
        self.lineView.hidden = YES;
    } else if (self.isFirstCell){
        self.pointView.image = [UIImage imageNamed:@"first_red"];
        self.lineView.hidden = NO;
    } else {
        self.pointView.image = [UIImage imageNamed:@"middle_red"];
        self.lineView.hidden = NO;
    }
}

#pragma mark - <UICollectionView代理>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.imagesData.count == 0) {
        
        return 1;
        
    } else {
        
        return self.imagesData.count;
        
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WDDetailImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.materData = self.imagesData[indexPath.row];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(cellItemW, 102);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *images = [NSMutableArray array];
    WDData *materData;
    NSString *imageURL;
    for (materData in self.imagesData) {
        imageURL = materData.url;
        [images addObject:imageURL];
    }
    
    [_delegate loanDetailShowOriginalImage:images clickIndex:indexPath.row];
    
    //WDLog(@"点击了第%zd个图片", indexPath.row);
}

@end
